package qp.src.app.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataBeanDao {

  private static Map<Integer/*ID*/, DataBean> dataBeanStore = new HashMap<Integer, DataBean>();

  private SubDataBeanDao subDataBeanDao;

  private Integer id = 0;

  public DataBeanDao(List<DataBean> datas, SubDataBeanDao subDataBeanDao){
    this.subDataBeanDao = subDataBeanDao;
    for( DataBean data : datas ){
      persistOnlyDataBean( data );
    }
  }

  public void persistOnlyDataBean(DataBean dataBean){
    if( dataBean.getId() == null ){
      dataBean.setId(id++);
      dataBean.setVersion(1);
    }
    else{
      dataBean.setVersion( dataBean.getVersion() + 1 );
    }

    DataBean existing = dataBeanStore.get( dataBean.getId() );
    if( existing != null ){
      System.out.println("--SERVER DataBeanDao persist dataBean name"+dataBean.getName());
      existing.copyFrom( dataBean );
    }
    else {
      System.out.println("--SERVER DataBeanDao persist dataBean !!ELSE name"+dataBean.getName()+ "   id = "+dataBean.getId());
      dataBeanStore.put(dataBean.getId(), dataBean);
    }
  }

  public void persist(DataBean dataBean){
    System.out.println("--SERVER DataBeanDao persists");
    if( dataBean.getId() == null ){
      dataBean.setId(id++);
      dataBean.setVersion(1);
    }
    else{
      dataBean.setVersion( dataBean.getVersion() + 1 );
    }

    //-------------------------------
    DataBean existing = dataBeanStore.get( dataBean.getId() );
    System.out.println("--SERVER DataBeanDao persist dataBean");
    if( dataBean.getData() != null ){
      subDataBeanDao.persist( dataBean.getData() );
    }
    if( existing != null ){
      System.out.println("--SERVER DataBeanDao persist dataBean name"+dataBean.getName());
      existing.copyFrom( dataBean );
    }
    else {
      System.out.println("--SERVER DataBeanDao persist dataBean !!ELSE name"+dataBean.getName()+ "   id = "+dataBean.getId());
      dataBeanStore.put(dataBean.getId(), dataBean);
    }
  }

  public void delete(DataBean dataBean){
    System.out.println("--SERVER DataBeanDao Start delete");
    if( dataBean.getData() != null ){
      Integer subDataBeanId = dataBean.getData().getId();
      if( subDataBeanId != null ){
        subDataBeanDao.delete( subDataBeanId );
      }
    }


    Integer dataBeanId = dataBean.getId();
    if( dataBeanId != null ){
      dataBeanStore.remove( dataBeanId );
    }
    System.out.println("--SERVER DataBeanDao End delete");
  }

  public List<DataBean> getDataBean(){
    System.out.println("--SERVER DataBeanDao getDataBean ");
    List<DataBean> result = new ArrayList<DataBean>();

    for( Integer id : dataBeanStore.keySet() ){
      result.add( dataBeanStore.get(id) );
    }
    System.out.println("--SERVER DataBeanDao getDataBean result size"+result.size());
    return result;
  }

  public DataBean findDataBean(Integer id){
    System.out.println("--SERVER DataBeanDao findDataBean id"+id );
    DataBean dataBean = null;
    if( id != null ){
      dataBean = dataBeanStore.get(id);
      if( dataBean != null ){
        SubDataBean subDataBean = subDataBeanDao.findSubDataBean(id);
        System.out.println("--SERVER DataBeanDao findDataBean subDataBean"+subDataBean);
        dataBean.setData( subDataBean );
        System.out.println("--SERVER DataBeanDao after findDataBean subDataBean"+subDataBean);
      }
    }
    System.out.println("--SERVER DataBeanDao findDataBean End id"+dataBean);
    return dataBean;
  }

}
